package org.example.sward.tooffer;

/**
 * <p>
 * Description: 动态规划，验证正则表达式
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2022/5/4 0004 22:24
 */
public class Offer19 {
    public static void main(String[] args) {
        Offer19 offer19 = new Offer19();
        boolean res = offer19.isMatch("aaa","ab*.*");
        System.out.println(res);
    }

    /**
     * 方法描述: 验证正则表达式
     *
     * @param s .
     * @param p .
     * @return boolean
     * @author 安路路
     * @date 2022/5/4 0004  22:24
     */
    public boolean isMatch(String s, String p) {
        int m = s.length() + 1, n = p.length() + 1;
        boolean[][] dp = new boolean[m][n];
        dp[0][0] = true;
        //初始化首行
        for(int j = 2; j < n; j++){
            dp[0][j] = dp[0][j - 2] && p.charAt(j - 1) == '*';
        }
        //状态转移
        for(int i = 1; i < m; i++){
            for (int j = 1; j < n; j++) {
                if(p.charAt(j - 1) == '*'){
                    if(dp[i][j-2]){
                        dp[i][j] = true;
                    }else if(dp[i-1][j] && s.charAt(i-1)==p.charAt(j-2)){
                        dp[i][j] = true;
                    }else if(dp[i-1][j] && p.charAt(j - 2) == '.'){
                        dp[i][j] = true;
                    }
                } else {
                    if(dp[i-1][j-1] && s.charAt(i - 1) == p.charAt(j - 1)){
                        dp[i][j] = true;
                    }else if(dp[i-1][j-1] && p.charAt(j - 1) == '.'){
                        dp[i][j] = true;
                    }
                }
            }
        }

        return dp[m-1][n-1];
    }
}
